package mashibing.class13;

import java.util.*;

/**
 * 二叉树最低祖先
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-22
 */
public class Class13_3_LowestAncestor {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node lowestAncestor1(Node node, Node a, Node b) {
        if (node == null) {
            return null;
        }
        // 遍历节点的父节点
        HashMap<Node, Node> map = new HashMap<>();
        map.put(node, null);
        fillPreMap(node, map);
        // 先遍历a，再遍历b是否包含
        Set<Node> set = new HashSet<>();
        Node cur = a;
        set.add(cur);
        while (map.get(cur) != null){
            set.add(map.get(cur));
            cur = map.get(cur);
        }
        cur = b;
        while (!set.contains(cur)){
            cur = map.get(cur);
        }
        return cur;
    }

    public static void fillPreMap(Node node, Map<Node, Node> map) {
        if (node.left != null) {
            map.put(node.left, node);
            fillPreMap(node.left, map);
        }
        if (node.right != null) {
            map.put(node.right, node);
            fillPreMap(node.right, map);
        }
    }

    public static class Info {
        public boolean findA;
        public boolean findB;
        public Node ans;

        public Info(boolean fa, boolean fb, Node an) {
            this.findA = fa;
            this.findB = fb;
            this.ans = an;
        }
    }

    public static Node lowestAncestor2(Node node, Node a, Node b) {
        if (node == null) {
            return null;
        }
        return process(node, a, b).ans;
    }

    public static Info process(Node node, Node a, Node b) {
        if (node == null) {
            return new Info(false, false, null);
        }
        Info leftInfo = process(node.left, a, b);
        Info rightInfo = process(node.right, a, b);
        boolean findA = node == a || leftInfo.findA || rightInfo.findA;
        boolean findB = node == b || leftInfo.findB || rightInfo.findB;
        Node ans = null;
        if (leftInfo.ans != null) {
            ans = leftInfo.ans;
        } else if (rightInfo.ans != null) {
            ans = rightInfo.ans;
        } else {
            if (findA && findB) {
                ans = node;
            }
        }
        return new Info(findA, findB, ans);
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    // for test
    public static Node pickRandomOne(Node head) {
        if (head == null) {
            return null;
        }
        ArrayList<Node> arr = new ArrayList<>();
        fillPrelist(head, arr);
        int randomIndex = (int) (Math.random() * arr.size());
        return arr.get(randomIndex);
    }

    // for test
    public static void fillPrelist(Node head, ArrayList<Node> arr) {
        if (head == null) {
            return;
        }
        arr.add(head);
        fillPrelist(head.left, arr);
        fillPrelist(head.right, arr);
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            Node o1 = pickRandomOne(head);
            Node o2 = pickRandomOne(head);
            if (lowestAncestor1(head, o1, o2) != lowestAncestor2(head, o1, o2)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }

}
